#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

from config import email as from_addr, password, to_addr, smtp_server

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


msg = MIMEText('这是我使用SMTP发送的邮件，邮件客户端因为会强制使用SSL不会抓包，只好出此下策', 'plain', 'utf-8')
msg['From'] = _format_addr('{}'.format(from_addr))
msg['To'] = _format_addr('{}' .format( to_addr))
msg['Subject'] = Header('计算机网络课程设计SMTP抓包实验', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()